package com.pay.cloud.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import com.pay.cloud.KeyInfo;
import com.pay.cloud.R;
import com.pay.cloud.ScanListener;
import com.pay.cloud.StatusListener;
import com.pay.cloud.fragment.PwdValidateFragment;
import com.pay.cloud.fragment.PwdValidateFragment.OnPwdListener;
import com.pay.cloud.fragment.ShopBindFragment;
import com.pay.cloud.fragment.ShopBindScanFragment;
import com.pay.cloud.fragment.ShopInfoFragment;
import com.pay.cloud.fragment.ShopMenuFragment;
import com.pay.cloud.fragment.ShopShiftFragment;
import com.pay.cloud.fragment.ShopStaffIdFragment;
import com.pay.cloud.fragment.ShopStaffIdFragment.OnStaffIdListner;
import com.pay.cloud.fragment.ShopUnbindFragment;
import com.pay.cloud.fragment.TipVerticalFragment;

import static com.pay.cloud.KeyInfo.KEY_CANCEL;
import static com.pay.cloud.KeyInfo.KEY_ENTER;
import static com.pay.cloud.KeyInfo.KEY_NUM_1;
import static com.pay.cloud.KeyInfo.KEY_NUM_2;
import static com.pay.cloud.KeyInfo.KEY_NUM_3;
import static com.pay.cloud.KeyInfo.KEY_NUM_4;
import static com.pay.cloud.fragment.TipFragment.TipType.SUCCESS;

public class ShopActivity extends AbstractActivity implements ScanListener, OnPwdListener, OnStaffIdListner, StatusListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        setMainFragment(new ShopMenuFragment());
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {

        if (mainFragment instanceof ShopMenuFragment) {
            if (keyInfo == KEY_NUM_1) {
                setMainFragment(new ShopStaffIdFragment(this));
            } else if (keyInfo == KEY_NUM_2) {
                setMainFragment(new ShopInfoFragment());
            } else if (keyInfo == KEY_NUM_3) {
                ShopBindScanFragment bindFragment = new ShopBindScanFragment(this);
                setMainFragment(bindFragment);
            } else if (keyInfo == KEY_NUM_4) {
                PwdValidateFragment pwdFragment = new PwdValidateFragment();
                pwdFragment.setOnPwdValidateListener(this);
                setMainFragment(pwdFragment);
            } else if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
            }
        } else if (mainFragment instanceof ShopStaffIdFragment
                || mainFragment instanceof ShopBindFragment
                || mainFragment instanceof ShopUnbindFragment
                || mainFragment instanceof ShopShiftFragment) {

        } else if (mainFragment instanceof ShopBindScanFragment) {
            if (keyInfo == KEY_CANCEL) {
                setMainFragment(new ShopMenuFragment());
            }
        } else if (mainFragment instanceof PwdValidateFragment) {
            PwdValidateFragment fragment = ((PwdValidateFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            if (keyInfo == KEY_CANCEL) {
                setMainFragment(new ShopMenuFragment());
            }
        } else if (mainFragment instanceof ShopInfoFragment
                || mainFragment instanceof TipVerticalFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                setMainFragment(new ShopMenuFragment());
            }
        }
        return false;
    }

    @Override
    public boolean onScan(String code) {
        if (mainFragment instanceof ShopBindScanFragment) {
            ShopBindFragment fragment = new ShopBindFragment(code, this);
            setMainFragment(fragment);
        }
        return true;
    }

    @Override
    public void onPwdValid() {
        ShopUnbindFragment fragment = new ShopUnbindFragment(this);
        setMainFragment(fragment);
    }

    @Override
    public void onStaffIdFinish(String staffId) {
        Fragment fragment = new ShopShiftFragment(staffId, this);
        setMainFragment(fragment);
    }

    @Override
    public void onStaffIdCancel() {
        setMainFragment(new ShopMenuFragment());
    }

    @Override
    public void onSuccess(String msg) {
        setMainFragment(new TipVerticalFragment(SUCCESS, msg));
    }

}
